/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reguistrarce.java
 *
 * Created on 14-nov-2011, 10:19:54
 */
package tsb_tp3.vista;

import javax.swing.JOptionPane;
import tsb_tp3.dao.DaoUsuario;
import tsb_tp3.dominio.Persona;
import tsb_tp3.dominio.Usuario;
import tsb_tp3.encriptacion.Encrypter;

/**
 *
 * @author Lucas
 */
public class Registrarse extends javax.swing.JFrame {

    /** Creates new form Reguistrarce */
    public Registrarse() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jtfNombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtfApellido = new javax.swing.JTextField();
        jtfRContraseña = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jbReguistar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jtfNumero = new javax.swing.JTextField();
        jtfContraseña = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cmb_Encriptacion = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Apellido : ");

        jLabel1.setText("Nombre : ");

        jLabel6.setText("tener menos de 6 caracteres");

        jLabel7.setText("y no mas de 10");

        jLabel8.setText("Repita contraseña: ");

        jbReguistar.setText("Registar");
        jbReguistar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbReguistarActionPerformed(evt);
            }
        });

        jLabel5.setText("La contraseña no debe ");

        jLabel3.setText("Numero : ");

        jLabel4.setText("Contreseña: ");

        jLabel9.setText("Método de encriptacion:");

        cmb_Encriptacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CesarEncrypter", "RCAEncrypter", "TranspositionEncrypter", "VernamEncrypter", "VernamMauborgneEncrypter", "VigenereEncrypter" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(188, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(241, Short.MAX_VALUE)
                .addComponent(jbReguistar)
                .addGap(216, 216, 216))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(cmb_Encriptacion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfRContraseña)
                            .addComponent(jtfContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jtfContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jtfRContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(1, 1, 1)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cmb_Encriptacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jbReguistar)
                .addGap(77, 77, 77))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbReguistarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbReguistarActionPerformed
if(this.validarCampos())
{
    Usuario u = new Usuario();
    Persona p  = new Persona(this.jtfNombre.getText(),this.jtfApellido.getText(),Integer.valueOf(this.jtfNumero.getText()));
    u.setNombre(this.jtfNombre.getText()+' '+this.jtfApellido.getText());
    u.setPersona(p);
    
    try{
    Encrypter enc=(Encrypter)Class.forName(cmb_Encriptacion.getSelectedItem().toString()).newInstance();
    enc.setMessage(this.jtfContraseña.getText());
    u.setPassword(enc.code());
    }catch(Exception c)
    {
        System.out.println("ClassNotFound");
    }
    
    
    
 

new DaoUsuario().guardar(u);
}   
}//GEN-LAST:event_jbReguistarActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmb_Encriptacion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbReguistar;
    private javax.swing.JTextField jtfApellido;
    private javax.swing.JTextField jtfContraseña;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JTextField jtfNumero;
    private javax.swing.JTextField jtfRContraseña;
    // End of variables declaration//GEN-END:variables

    public void limpiarCampos()
    {
        jtfNumero.setText("");
        jtfNombre.setText("");
        jtfApellido.setText("");
        jtfContraseña.setText("");
        jtfRContraseña.setText("");
    }
    
    public boolean validarCampos()
    {
        if(this.isNombre()==false)
        {            
            return false;
        }
        if(this.isApellido()==false)
        {            
            return false;
        }
        if(this.isNumero()==false)
        {            
            return false;
        }
        if(this.isContraseña()==false)
        {
            return false;
        }
        return true;
        
    }
    public boolean isNombre()
    {
        char c=' ';
        if(jtfNombre.getText().isEmpty()==false)
        {
            for(int i=0;i<jtfNombre.getText().length();i++)
            {
                c=jtfNombre.getText().charAt(i);
                if(Character.isDigit(c))
                {
                    JOptionPane.showMessageDialog(null,"Lo ingresado no es un nombre valido.", "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null,"No ingreso un nombre.", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public boolean isApellido()
    {
        char c=' ';
        if(jtfApellido.getText().isEmpty()==false)
        {
            for(int i=0;i<jtfApellido.getText().length();i++)
            {
                c=jtfApellido.getText().charAt(i);
                if(Character.isDigit(c))
                {
                    JOptionPane.showMessageDialog(null,"Lo ingresado no es un apellido valido.", "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null,"No ingreso un apellido.", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public boolean isNumero()
    {
        char c=' ';
        if(jtfNumero.getText().isEmpty()==false)
        {
            for(int i=0;i<jtfNumero.getText().length();i++)
            {
                c=jtfNumero.getText().charAt(i);
                if(Character.isDigit(c)==false)
                {
                    JOptionPane.showMessageDialog(null,"Lo ingresado no es numerico.", "Error", JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null,"No ingreso un numero de cliente.", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public boolean isContraseña()
    {
        if(jtfContraseña.getText().isEmpty()==false && jtfRContraseña.getText().isEmpty()==false)
        {
            if(jtfContraseña.getText().compareTo(jtfRContraseña.getText())==-1)
            {
                JOptionPane.showMessageDialog(null,"Las contraseñas no son las mismas.", "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            return false;
        }
        else
        {
            if(jtfContraseña.getText().isEmpty())
            {
                JOptionPane.showMessageDialog(null,"No ingreso una contraseña.", "Error", JOptionPane.ERROR_MESSAGE);
            }
            if(jtfRContraseña.getText().isEmpty())
            {
                JOptionPane.showMessageDialog(null,"No repitio la contraseña.", "Error", JOptionPane.ERROR_MESSAGE);
            }               
            return false;
        }
        
    }
    
  
    

}

